import { type RouteRecordRaw, createRouter, createWebHashHistory, createWebHistory } from "vue-router"

const Layout = () => import("@/layout/index.vue")

/** 常驻路由 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layout,
    meta: {
      hidden: true
    },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue")
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/views/error-page/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/views/error-page/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Dashboard",
        meta: {
          title: "首页",
          svgIcon: "dashboard",
          affix: true
        }
      }
    ]
  }
]

/**
 * 动态路由
 * 用来放置有权限 (Roles 属性) 的路由
 * 必须带有 Name 属性
 */
export const asyncRoutes: RouteRecordRaw[] = [
  {
    path: "/",
    component: Layout,
    redirect: "/department",
    name: "department",
    meta: {
      roles: ["2"],
      title: "办学单位信息管理",
      auth: "department.query",
      svgIcon: "department",
      affix: false
    },
    children: [
      {
        path: "department",
        component: () => import("@/views/department/index.vue"),
        name: "_department",
        meta: {
          roles: ["2"],
          auth: "department.query",
          title: "办学单位信息管理",
          svgIcon: "department",
          affix: false
        }
      }
    ]
  },

  {
    path: "/",
    component: Layout,
    redirect: "/finishSchool",
    name: "finishSchool",
    meta: {
      roles: ["0"],
      title: "结业信息",
      svgIcon: "finishSchool",
      affix: false
    },
    children: [
      {
        path: "finishSchool",
        component: () => import("@/views/finish-school/index.vue"),
        name: "_finishSchool",
        meta: {
          roles: ["0"],
          title: "结业信息",
          svgIcon: "finishSchool",
          affix: false
        }
      }
    ]
  },
  {
    path: "/",
    component: Layout,
    redirect: "/admin",
    name: "admin",
    meta: {
      roles: ["2"],
      auth: "admin.query",
      title: "管理员信息管理",
      svgIcon: "admin",
      affix: false
    },
    children: [
      {
        path: "admin",
        component: () => import("@/views/admin/index.vue"),
        name: "_admin",
        meta: {
          roles: ["2"],
          auth: "admin.query",
          title: "管理员信息管理",
          svgIcon: "admin",
          affix: false
        }
      }
    ]
  },

  {
    path: "/",
    component: Layout,
    redirect: "/project",
    name: "project",
    meta: {
      title: "项目信息管理",
      roles: ["1", "2"],
      auth: "project.query",
      svgIcon: "project",
      affix: false
    },
    children: [
      {
        path: "project",
        component: () => import("@/views/project/index.vue"),
        name: "_project",
        meta: {
          title: "项目信息管理",
          roles: ["1", "2"],
          auth: "project.query",
          svgIcon: "project",
          affix: false
        }
      }
    ]
  },
  {
    path: "/",
    component: Layout,
    redirect: "/grade",
    name: "grade",
    meta: {
      title: "班级信息管理",
      roles: ["1", "2"],
      auth: "class.query",
      svgIcon: "grade",
      affix: false
    },
    children: [
      {
        path: "grade",
        component: () => import("@/views/grade/index.vue"),
        name: "_grade",
        meta: {
          title: "班级信息管理",
          roles: ["1", "2"],
          auth: "class.query",
          svgIcon: "grade",
          affix: false
        }
      }
    ]
  },
  {
    path: "/",
    component: Layout,
    name: "student",
    redirect: "/student",
    meta: {
      title: "学员信息管理",
      auth: "student.query",
      svgIcon: "trainee",
      roles: ["1", "2"],
      affix: false
    },
    children: [
      {
        path: "student",
        component: () => import("@/views/student/index.vue"),
        name: "_student",
        meta: {
          auth: "student.query",
          title: "学员信息管理",
          svgIcon: "trainee",
          roles: ["1", "2"],
          affix: false
        }
      }
    ]
  },

  {
    path: "/file",
    component: Layout,
    redirect: "/file/view",
    name: "file",
    meta: {
      auth: "file.query",
      title: "资料管理",
      svgIcon: "files",
      roles: ["1", "2"], // 可以在根路由中设置角色
      alwaysShow: true // 将始终显示根菜单
    },
    children: [
      {
        path: "upload",
        component: () => import("@/views/files/upload/index.vue"),
        name: "upload",
        meta: {
          title: "上传资料",
          elIcon: "upload",
          auth: "file.upload",
          affix: false,
          roles: ["1", "2"]
        }
      },
      {
        path: "view",
        name: "view",
        component: () => import("@/views/files/view/index.vue"),
        meta: {
          title: "浏览资料",
          elIcon: "view",
          affix: false,
          roles: ["1", "2"]
        }
      },
      {
        path: "audit",
        name: "audit",
        component: () => import("@/views/files/audit/index.vue"),
        meta: {
          title: "审核资料",
          elIcon: "check",
          auth: "file.check",
          affix: false,
          roles: ["2"]
        }
      }
    ]
  },
  {
    path: "/",
    component: Layout,
    name: "necessary",
    meta: {
      title: "必要资料上传管理",
      roles: ["2"],
      svgIcon: "necessary",
      auth: "fileItem.query",
      affix: false
    },
    children: [
      {
        path: "necessary",
        component: () => import("@/views/necessary/index.vue"),
        name: "_necessary",
        meta: {
          title: "必要资料上传管理",
          roles: ["2"],
          auth: "fileItem.query",
          svgIcon: "necessary",
          affix: false
        }
      }
    ]
  },
  {
    path: "/",
    component: Layout,
    redirect: "/system",
    name: "system",
    meta: {
      title: "变量管理",
      auth: "variable.query",
      roles: ["2"],
      svgIcon: "system",
      affix: false
    },
    children: [
      {
        path: "system",
        component: () => import("@/views/system/index.vue"),
        name: "_system",
        meta: {
          title: "变量管理",
          roles: ["2"],
          auth: "variable.query",
          svgIcon: "system",
          affix: false
        }
      }
    ]
  },
  {
    path: "/",
    component: Layout,
    redirect: "/operation",
    name: "operation",
    meta: {
      title: "运维管理",
      auth: "pw.query",
      roles: ["2"],
      svgIcon: "operation",
      affix: false
    },
    children: [
      {
        path: "operation",
        component: () => import("@/views/operation/index.vue"),
        name: "_operation",
        meta: {
          title: "运维管理",
          roles: ["2"],
          auth: "pw.query",
          svgIcon: "operation",
          affix: false
        }
      }
    ]
  },
  {
    path: "/",
    component: Layout,
    redirect: "/message",
    name: "message",
    meta: {
      title: "消息通知",
      svgIcon: "operation",
      affix: false
    },
    children: [
      {
        path: "message",
        component: () => import("@/views/message/index.vue"),
        name: "_message",
        meta: {
          title: "消息通知",
          svgIcon: "message",
          affix: false
        }
      }
    ]
  },
  {
    path: "/:pathMatch(.*)*", // Must put the 'ErrorPage' route at the end, 必须将 'ErrorPage' 路由放在最后
    redirect: "/404",
    name: "ErrorPage",
    meta: {
      hidden: true
    }
  }
]

const router = createRouter({
  history:
    import.meta.env.VITE_ROUTER_HISTORY === "hash"
      ? createWebHashHistory(import.meta.env.VITE_PUBLIC_PATH)
      : createWebHistory(import.meta.env.VITE_PUBLIC_PATH),
  routes: constantRoutes
})

/** 重置路由 */
export function resetRouter() {
  // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
  try {
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch (error) {
    // 强制刷新浏览器也行，只是交互体验不是很好
    window.location.reload()
  }
}

export default router
